//------------------------------------------------------------------------------
//	file dsp_function.c
//
//	brief
//		Copy input to output buffer.
//
//	Description
//		This DSP effect file shall support numerous DSP experiments without
//		recoding the effect over and over.
//
//	$Id: dsp_function.c 294 2014-08-26 03:51:04Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//	The MIT License (MIT)
//
//	Copyright (c) 2014 A.C. Verbeck
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.

//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <stdint.h>
#include <stdbool.h>

#include <sndfile.h>

#include "dsp_function.h"

//------------------------------------------------------------------------------
// Local data
//------------------------------------------------------------------------------
static SF_INFO	o_sf;
static PARAM_T	p_save;

//------------------------------------------------------------------------------
//	brief
//		parameter init / processing / end functions
//
//	Description
//		These functions are called as follows:
//		dsp_param_init:		is called at the start of DSP processing
//		dsp_param_process:	is called after each audio buffer is processed
//		dsp_param_end:		is called at the end of DSP processing
//
//	param
//		none
//
//	return
//		none
//------------------------------------------------------------------------------
void dsp_param_init(PARAM_T* p_in)
{
	p_save = *p_in;

	printf("param display:\n");
	printf("\tAttenuation: %f\n", p_save.amp);
	printf("\tSample Rate: %d\n", p_save.sr);
	printf("\tTime in seconds: %d\n", p_save.time);
}

void dsp_param_process(void)
{
}

void dsp_param_end(void)
{
}

//------------------------------------------------------------------------------
//	brief
//		dsp buffer init function
//
//	param
//		SF_INFO* sfinfo: Pointer to a sound-file information structure
//
//	return
//		none
//------------------------------------------------------------------------------

void dsp_buff_init(SF_INFO* o_sfinfo)
{
	o_sf = *o_sfinfo;															//	Save output sound file info

	if (o_sf.channels != 2) {													//	How many channels does this effect support
		printf("This DSP effect can only generate stereo files\n");
		exit(0);
	}
	if ((o_sf.format & SF_FORMAT_SUBMASK) != SF_FORMAT_FLOAT) {					//	Data format
		printf("This DSP effect can only generate 32-bit floating point files\n");
		exit(0);
	}
	if ((o_sf.format & SF_FORMAT_ENDMASK) != SF_ENDIAN_FILE) {					//	Endian file types
		printf("This DSP effect can only generate native endian files\n");
		exit(0);
	}
}

//------------------------------------------------------------------------------
//	brief
//		dsp buffer processing function
//
//	param
//		double*		b_out:	Buffer to place the modified data
//		double*		b_in:	Incoming audio data buffer
//		sf_count_t	ct:		Number of items to process in this function
//
//	return
//		none
//------------------------------------------------------------------------------
void dsp_buff_process(double* b_out, double* b_in, sf_count_t ct)
{
	double l_val, r_val;

	for (sf_count_t i=0; i<ct; i++)	{											//	Loop through the input buffer
		l_val = *b_in++;														//	Read the left sample
		r_val = *b_in++;														//	Read the right sample

		*b_out++ = l_val;														//	Write the left output value
		*b_out++ = r_val;														//	Write the right output value
	}
}

void dsp_buff_end(void)
{
}

//
//	End: dsp_function.c
//
